﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TaskManager
{
    // 签到逻辑
    public class Sign
    {
        // 判定后台数据库中是否已经保存了用户的登录信息，若未保存则自动插入
        public static void confirmCheckIn()
        {
            try
            {
                String date = Tool.getDate(DateTime.Now);       // 获取日期，如 2005-06-07
                //date = "2016-09-28";

                // 记录签到信息
                String Str = Tool.RegistryStrValue(@"Scimence\TaskManager\Set", "签到时间");
                if (!Str.StartsWith(date))                          // 今天未签到
                {
                    String account = loginForm.userName;            // 登录名称

                    // 查询今天的签到信息
                    // SELECT * FROM `zq_checking_in` WHERE `account`='wang.zhy' AND `date`='2016-09-27'
                    String sql = "SELECT * FROM `zq_checking_in` WHERE `account`='" + account + "' AND `date`='" + date + "'";
                    String data = Tool.Execute(sql);
                    List<Data_ChecingIn> Iteams = Data_ChecingIn.Iteams(data);

                    if (Iteams.Count == 0)
                    {
                        String createDate = Tool.TimeNow();             // 获取当前时间如： 2005-06-07 12:23:34
                        String realname = account;                      // 真实姓名
                        String localIp = Tool.GetLocalIp();             // 获取内网ip
                        String extenalIp = Tool.GetExtenalIpAddress();  // 获取外网ip

                        // 查询真实用户名信息
                        sql = "SELECT * FROM `zt_user` WHERE `account`='" + account + "'";
                        data = Tool.Execute(sql);
                        List<Data_User> list = Data_User.Iteams(data);
                        if (list.Count > 0) realname = list[0].realname;// 查询到的用户名

                        // 在数据库中插入签到记录
                        string str1 = "INSERT INTO `zq_checking_in`(account, realname, date, createDate, nei_ip, wai_ip, version)";
                        string str2 = "VALUES('" + account + "', '" + realname + "', '" + date + "', '" + createDate + "', '" + localIp + "', '" + extenalIp + "', '" + Program.curVersion + "')";
                        sql = str1 + str2;

                        data = Tool.Execute(sql);
                    }
                    else Tool.RegistrySave(@"Scimence\TaskManager\Set", "签到时间", Iteams[0].createDate); // 在注册表中记录签到时间
                }
            }
            catch { }
        }
    
    }
}
